package com.taoyibao.hongcanting.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.event.EventUpdataTask;
import com.taoyibao.hongcanting.model.TaskModel;
import com.taoyibao.hongcanting.utils.CodeUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import static android.view.View.VISIBLE;

/**
 * @author TangAnna
 * @description: 屏幕操作员列表的适配器
 * @date :${DATA} 11:29
 */
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.MyViewHolder> {

    private List<TaskModel> mData;
    //存放每一个holder的集合
    private List<TaskAdapter.MyViewHolder> mHolderList = new ArrayList<>();
    private Context mContext;

    public TaskAdapter(List<TaskModel> data, Context context) {
        mData = data;
        mContext = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_admin, null, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int position) {
        myViewHolder.setDataPosition(position);

        //先判断集合中是否包含当前的ViewHolder
        if (!mHolderList.contains(myViewHolder)) {
            mHolderList.add(myViewHolder);//添加
        }
        //设置数据
        setData(myViewHolder, mData.get(position), position);

        myViewHolder.mBtnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mData != null && mData.size() > 0) {
                    //发送消息到AdministratorFragment中调用接口更新任务状态  刷新页面
                    EventUpdataTask task = new EventUpdataTask();
                    task.mTaskModel = mData.get(position);
                    if (mData.get(position).status == 1) {//确认操作
                        task.status = 2;
                    } else if (mData.get(position).status == 2) {//完成操作
                        task.status = 3;
                    }
                    EventBus.getDefault().post(task);
                }
            }
        });
    }

    /**
     * 设置数据
     *
     * @param holder
     * @param data
     * @param position
     */
    private void setData(MyViewHolder holder, TaskModel data, int position) {
        //根据不同状态先控制页面显示
        switch (data.status) {
            case CodeConstant.TASK_STATUS_NEW://新任务
                holder.mBtnConfirm.setVisibility(View.VISIBLE);
                holder.mBtnConfirm.setText(CodeUtils.getIdString(R.string.admin_confirm));
                holder.mBtnConfirm.setBackgroundResource(R.drawable.shape_bg_raduis_ffd);
                //计时器
                holder.mTvTimer.setVisibility(VISIBLE);
                holder.mTvTimer.setText(data.timer);
                //发送时间
                holder.mTvSendTime.setText(CodeUtils.formatDateSecond(data.create_at));
                break;
            case CodeConstant.TASK_STATUS_ING://进行中
                holder.mBtnConfirm.setVisibility(View.VISIBLE);
                holder.mBtnConfirm.setText(CodeUtils.getIdString(R.string.admin_complete));
                holder.mBtnConfirm.setBackgroundResource(R.drawable.shape_bg_raduis_ff9);
                //计时器
                holder.mTvTimer.setVisibility(VISIBLE);
                holder.mTvTimer.setText(data.timer);
                //更新时间
                holder.mTvSendTime.setText(CodeUtils.formatDateSecond(data.update_at));
                break;
            case CodeConstant.TASK_STATUS_FINISH://已完成
                holder.mBtnConfirm.setVisibility(View.GONE);
                holder.mTvTimer.setVisibility(View.GONE);
                //更新时间
                holder.mTvSendTime.setText(CodeUtils.formatDateSecond(data.update_at));
                break;
        }
        //设置序号
        holder.mTvNub.setText((position + 1) + "");
        //桌号
        if (!TextUtils.isEmpty(data.desk_code)) {
            holder.mTvTable.setText(data.desk_code);
        } else {   //游客
            holder.mTvTable.setText(CodeUtils.getIdString(R.string.admin_tourist));
        }

        //礼物类型
        if (TextUtils.equals(data.type, "大屏")) {//大屏
            holder.mLayoutBaPing.setVisibility(VISIBLE);
            if (!TextUtils.isEmpty(data.content)) {
                holder.mTvBaPingText.setText(data.content);
            }
            holder.mTvTypeAndDuration.setText("时长:");
            holder.mTvTypeAndDurationValue.setText(data.gift_name + "分钟");
        } else {//正常礼物
            holder.mLayoutBaPing.setVisibility(View.GONE);
            holder.mTvTypeAndDuration.setText(data.type + ":");
            holder.mTvTypeAndDurationValue.setText(data.gift_name);
        }
    }

    /**
     * 只刷新item中的计时器数据
     */
    public void notifyTime() {
        if (mHolderList != null && mHolderList.size() > 0) {
            for (int i = 0; i < mHolderList.size(); i++) {
                if (i < mData.size() && i < mHolderList.size()) {
                    mHolderList.get(i).mTvTimer.setText(mData.get(mHolderList.get(i).position).timer);
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        /**
         * 任务状态
         */
        private int mStatus;

        /**
         * 序号
         */
        private TextView mTvNub;
        /**
         * 桌号
         */
        private TextView mTvTable;
        /**
         * 发送时间
         */
        private TextView mTvSendTime;
        /**
         * 任务种类和时长  title
         */
        private TextView mTvTypeAndDuration;
        /**
         * 任务种类 和时长的值
         */
        private TextView mTvTypeAndDurationValue;
        /**
         * 霸屏最外层布局
         */
        private LinearLayout mLayoutBaPing;
        /**
         * 霸屏的文字
         */
        private TextView mTvBaPingText;
        /**
         * 计时器
         */
        private TextView mTvTimer;
        /**
         * 确认按钮
         */
        private Button mBtnConfirm;
        private int position;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            mTvNub = itemView.findViewById(R.id.tv_itemAdmin_nub);
            mTvTable = itemView.findViewById(R.id.tv_itemAdmin_table);
            mTvSendTime = itemView.findViewById(R.id.tv_itemAdmin_sendTime);
            mTvTypeAndDuration = itemView.findViewById(R.id.tv_itemAdmin_typeAndDuration);
            mTvTypeAndDurationValue = itemView.findViewById(R.id.tv_itemAdmin_typeAndDurationValue);
            mLayoutBaPing = itemView.findViewById(R.id.layout_itemAdmin_baPing);
            mTvBaPingText = itemView.findViewById(R.id.tv_itemAdmin_baPingText);
            mTvTimer = itemView.findViewById(R.id.tv_itemAdmin_timer);
            mBtnConfirm = itemView.findViewById(R.id.btn_admin_confirm);
        }

        /**
         * 绑定position
         *
         * @param position
         */
        public void setDataPosition(int position) {
            this.position = position;
        }

    }
}
